package com.gene.service.impl;

import com.gene.mapper.UserCartMapper;
import com.gene.pojo.UserCart;
import com.gene.pojo.UserCartExample;
import com.gene.pojo.entity.CartEntity;
import com.gene.service.IUserCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserCartServiceImpl implements IUserCartService {
    @Autowired
    private UserCartMapper userCartMapper;

    /**
     * 查询   查询条件为userId(用户id)
     * @param userCart
     * @return
     */
    @Override
    public List<CartEntity> query(UserCart userCart) {
        Integer userId=userCart.getUserId();
        List<CartEntity> list=userCartMapper.query(userId);

        return list;
    }

    @Override
    public UserCart selectId(UserCart userCart) {

        UserCart u = userCartMapper.selectByPrimaryKey(userCart.getCartId());
        return u;
    }

    /**
     * 新增
     * @param userCart
     * @return
     */
    @Override
    public boolean add(UserCart userCart) {
        userCart.setCreateTime(new Date());//获取当前系统时间

        if(userCart.getStatus()==null){
            userCart.setStatus((byte)1);
        }
        int a=userCartMapper.insert(userCart);
        if(a==1){
            return true;
        }
        return false;
    }

    /**
     * 修改
     * @param userCart
     * @return
     */
    @Override
    public boolean edit(UserCart userCart) {
        userCart.setUpdateTime(new Date());//获取当前系统时间
        int a=userCartMapper.updateByPrimaryKeySelective(userCart);
        if(a==1){
            return true;
        }
        return false;
    }

    /**
     * 批量修改
     * @param list
     * @return
     */
    @Override
    @Transactional
    public boolean editList(List<UserCart> list) {
        if(list.size()>0){
            for (UserCart u: list) {
                u.setUpdateTime(new Date());
                int a=userCartMapper.updateByPrimaryKeySelective(u);
                if(a!=1){
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//设置手动回滚事务
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    /**
     * 根据id删除
     * @param userCart
     * @return
     */
    @Override
    public boolean delete(UserCart userCart) {
        int a=userCartMapper.deleteByPrimaryKey(userCart.getCartId());
        if(a==1){
            return true;
        }
        return false;
    }


    /**
     * 批量删除
     * 1：根据userId查询出list
     * 2: 循环list把cartId传入
     * 3：删除
     * @param userCart
     * @return
     */
    @Transactional
    @Override
    public boolean deleteList(UserCart userCart) {
        List<CartEntity> list=userCartMapper.query(userCart.getUserId());//查询userId下所有的购物车
        UserCartExample example=new UserCartExample();
        UserCartExample.Criteria criteria = example.createCriteria();

        List<Integer> idList=new ArrayList<>();

        if(list.size()>0){
            for (CartEntity entity :list) {
                idList.add(entity.getCartId());
            }
            criteria.andCartIdIn(idList);
            int a=userCartMapper.deleteByExample(example);
            if(a>0){
                return true;
            }
        }

        return false;
    }


}
